package main

import (
	"image/color"
	"time"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
)

func main() {
	a := app.New()
	mainWindow := a.NewWindow("aoi")
	mainWindow.Resize(fyne.NewSize(900, 650))
	mainWindow.SetFixedSize(true)
	mainWindow.CenterOnScreen()
	mainContainer := container.NewWithoutLayout()
	drawGrid(mainContainer)
	drawText(mainContainer)
	drawCircle(mainContainer)

	btn := widget.NewButton("动画", func() {
		drawAnimation(mainContainer)
	})
	btn.Resize(fyne.NewSize(100, 30))
	btn.Move(fyne.NewPos(30, 80))
	mainContainer.Add(btn)
	mainWindow.SetContent(mainContainer)
	mainWindow.ShowAndRun()
}

// 动画
func drawAnimation(container *fyne.Container) {
	circle := canvas.NewCircle(color.RGBA{R: 0, G: 255, B: 255, A: 255})
	circle.Position1 = fyne.NewPos(100, 100)
	circle.Position2 = fyne.NewPos(150, 150)
	ani := canvas.NewPositionAnimation(fyne.NewPos(100, 100), fyne.NewPos(600, 600), time.Second*10, func(pos fyne.Position) {
		circle.Move(pos)
	})
	ani.Curve = fyne.AnimationEaseInOut
	ani.Start()
	container.Add(circle)
}

// 画文本
func drawText(container *fyne.Container) {
	text := canvas.NewText("当前单位数:0", color.RGBA{R: 0, G: 0, B: 255, A: 255})
	text.TextSize = 16
	text.Alignment = fyne.TextAlignCenter
	text.TextStyle = fyne.TextStyle{
		Bold:      true,
		Italic:    true,
		Monospace: true,
		TabWidth:  20,
	}
	text.Move(fyne.NewPos(60, 30))
	container.Add(text)
}

// 画圆
func drawCircle(container *fyne.Container) {
	circle := canvas.NewCircle(color.RGBA{R: 0, G: 0, B: 255, A: 255})
	circle.Position1 = fyne.NewPos(100, 100)
	circle.Position2 = fyne.NewPos(150, 150)
	container.Add(circle)
}

// 画格子
func drawGrid(container *fyne.Container) {
	cellw := 20
	width := 700
	height := 600
	for j := 0; j <= height/cellw; j++ {
		line := canvas.NewLine(color.Black)
		line.Position1 = fyne.NewPos(float32(150), float32(j*cellw+20))
		line.Position2 = fyne.NewPos(float32(850), float32(j*cellw+20))
		line.StrokeWidth = 1
		container.Add(line)
	}
	for j := 0; j <= width/cellw; j++ {
		line := canvas.NewLine(color.Black)
		line.Position1 = fyne.NewPos(float32(j*cellw+150), float32(20))
		line.Position2 = fyne.NewPos(float32(j*cellw+150), float32(620))
		line.StrokeWidth = 1
		container.Add(line)
	}
}
